package com.example.web.sys_role.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.utils.ResultUtils;
import com.example.utils.ResultVo;
import com.example.web.sys_role.entity.Role;
import com.example.web.sys_role.entity.RoleParam;
import com.example.web.sys_role.entity.SaveMenuParam;
import com.example.web.sys_role.entity.SelectItem;
import com.example.web.sys_role.service.IRoleService;
import com.example.web.sys_role_menu.service.IRoleMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author LS
 * @since 2024-01-19
 */
@Api(tags = "角色接口")
@RestController
@RequestMapping("/api/role")
public class RoleController {

    @Resource
    private IRoleService roleService;
    @Resource
    private IRoleMenuService roleMenuService;

    @ApiOperation("新增")
    @PostMapping
    public ResultVo add(@RequestBody Role role) {
        role.setCreateTime(new Date());
        if (roleService.save(role)) {
            return ResultUtils.success("新增成功!");
        }
        return ResultUtils.error("新增失败!");
    }

    @ApiOperation("编辑")
    @PutMapping
    public ResultVo edit(@RequestBody Role role) {
        role.setUpdateTime(new Date());
        if (roleService.updateById(role)) {
            return ResultUtils.success("编辑成功!");
        }
        return ResultUtils.error("编辑失败!");
    }

    @ApiOperation("删除")
    @DeleteMapping("/{roleId}")
    public ResultVo delete(@PathVariable("roleId") Long roleId) {
        if (roleService.removeById(roleId)) {
            return ResultUtils.success("删除成功!");
        }
        return ResultUtils.error("删除失败!");
    }

    @ApiOperation("列表")
    @GetMapping("/getList")
    public ResultVo getList(RoleParam param) {
        //构造分页对象
        IPage<Role> page = new Page<>(param.getCurrentPage(), param.getPageSize());
        //构造查询条件
        QueryWrapper<Role> query = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(param.getRoleName())) {
            query.lambda().like(Role::getRoleName, param.getRoleName());
        }
        query.lambda().orderByDesc(Role::getCreateTime);
        IPage<Role> list = roleService.page(page, query);
        return ResultUtils.success("查询成功", list);
    }

    @ApiOperation("角色下拉数据")
    @GetMapping("/selectList")
    public ResultVo selectList(){
        List<Role> list = roleService.list();
        //返回的值
        List<SelectItem> selectItems = new ArrayList<>();
        Optional.ofNullable(list).orElse(new ArrayList<>())
                .forEach(item ->{
                    SelectItem vo = new SelectItem();
                    vo.setCheck(false);
                    vo.setLabel(item.getRoleName());
                    vo.setValue(item.getRoleId());
                    selectItems.add(vo);
                });
        return  ResultUtils.success("查询成功",selectItems);
    }

    @ApiOperation("保存角色菜单")
    @PostMapping("/saveRoleMenu")
    public ResultVo saveRoleMenu(@RequestBody SaveMenuParam param){
        roleMenuService.saveRoleMenu(param);
        return ResultUtils.success("分配成功");
    }

}

